package com.taoism.utils;

/**
 * 线程全局工具获取用户信息
 *
 * @author lijia
 * @version 1.0 @Description 线程全局工具获取用户信息
 * @date 2019/9/15 16:07 星期日
 */
public class ThreadUtil<T> {

    private static final ThreadLocal<String> tokenHolder = new ThreadLocal<>();

    private static final ThreadLocal<Object> userHolder = new ThreadLocal<>();

    /**
     * 获取token
     *
     * @return
     */
    public static String getToken() {
        return tokenHolder.get();
    }

    /**
     * 设置token
     *
     * @param token
     */
    public static void setToken(String token) {
        tokenHolder.set(token);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static <T> T getUserHolder() {
        return (T) userHolder.get();
    }

    /**
     * 设置用户信息
     *
     * @param userInfo
     */
    public static <T> void setUserHolder(T userInfo) {
        userHolder.set(userInfo);
    }

    /**
     * 移除缓存
     */
    public static void remove() {
        tokenHolder.remove();
        userHolder.remove();
    }
}
